import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Shapes 1.15



MainView {
    id: root
    width: 800
    height: 391
    property color btnColor: "#7080C0"
    property bool gainCalOK:false
    Component.onCompleted: {
        getVoltageCurrentTimer.interval = mymodbus.collectTime;
        getVoltageCurrentTimer.running = true;
    }
    Component.onDestruction: {
        getVoltageCurrentTimer.running = false;
        getVoltageCurrentTimer.destroy();
    }

    Rectangle
    {
        width:800
        height: 120
        color:"transparent"
    GridLayout{ //root中放置一个行布局，布局中放置3个方块
        id: zeroCalibrationLayout
        anchors.centerIn: parent
        columnSpacing: 20 //间距
        rows: 3//行数
        columns: 5 //列数
//        MyComboBox{
//            id:deviceIndex
//            Layout.row:0
//            Layout.column:0
//            Layout.preferredWidth: 160
//            Layout.preferredHeight: 30
//        }
        MyComboBox
        {
            id:cmbGainIndex
            Layout.preferredHeight: 30
            Layout.row: 0
            Layout.column: 0
            Layout.columnSpan: 2
            font.pixelSize: 20
            width: 160
            model:[qsTr("增益1"),qsTr("增益2"),qsTr("增益3"),qsTr("增益4")]
        }
//        Text
//        {
//            Layout.row: 1
//            Layout.column: 0
//            text:qsTr("砝码")
//            color:"white"
//            font.pixelSize: 20

//            Layout.preferredWidth: 80
//            Layout.preferredHeight: 30
//        }
//        Button
//        {
//            font.pixelSize: 20
//            Layout.row: 1
//            Layout.column: 1
//            Layout.preferredWidth: 80
//            Layout.preferredHeight: 30
//            id:hasWeight
//            text: qsTr("有")
//            background: Rectangle{
//                id:recHasWeight
//                border.width: 0
//                color: "lightgreen"
//                    }
//            onClicked: {
//                recHasWeight.color = "lightgreen"
//                recNoWeight.color = "darkgray"
//                btnCurValue.visible = false
//                recCurValue.visible = false
//                miniKeyBoard.visible = false;
//            }
//            palette.buttonText: "white"
//        }
//        Button
//        {
//            font.pixelSize: 20
//            Layout.row: 1
//            Layout.column: 2
//            Layout.preferredWidth: 80
//            Layout.preferredHeight: 30
//            id:noWeight
//            text: qsTr("无")
//            background: Rectangle{
//                id:recNoWeight
//                border.width: 0
//                color: "darkGray"
//                    }
//            onClicked: {
//                recHasWeight.color="darkgray"
//                recNoWeight.color = "lightgreen"
//                btnCurValue.visible = true
//                recCurValue.visible = true
//                miniKeyBoard.visible = true;
//            }
//            palette.buttonText: "white"
//        }
        Button
        {
            id: btnCurValue
            Layout.row:1
            Layout.column:0
            Layout.preferredWidth: 80
            Layout.preferredHeight: 30
            text: qsTr("当前值")
            font.pixelSize: 18
            background: Rectangle{
                color: btnColor
                    }
            onClicked: {
                //miniKeyBoard.lineValue = "";
                //miniKeyBoard.textInput=oldPwd;

            }
        }

        Rectangle {
            id: recCurValue
            //width:300
            //height:30
            // x: 218
            // y: 7
            color: "transparent"
            border.width: 0
            border.color: "white"
            Layout.row:1
            Layout.column:1
            Layout.columnSpan: 2
            Layout.preferredWidth: 180
            Layout.preferredHeight: 30
            // 绘制一条边框
            Shape {
                anchors.fill: parent
                ShapePath {
                    strokeWidth: 3
                    strokeColor: "white"
                    strokeStyle: ShapePath.SolidLine
                    startX: 0
                    startY: recCurValue.height
                    PathLine {
                        x: recCurValue.width
                        y: recCurValue.height
                    }
                }
            }
            TextInput{
                id:curValue
                anchors.fill: parent
                //echoMode: TextInput.Password
                color:"white"
                font.pixelSize: 25
                horizontalAlignment: Text.AlignHCenter
                readOnly: true
                MouseArea {
                       anchors.fill: parent
                       onClicked: {
                           // miniKeyBoard.lineValue = "";
                           // miniKeyBoard.textInput=oldPwd;
                       }
                   }

            }
        }

        // Button
        // {
        //     id: btnCurrent
        //     Layout.row:1
        //     Layout.column:2
        //     Layout.preferredWidth: 80
        //     Layout.preferredHeight: 35
        //     text: qsTr("电流")
        //     font.pixelSize: 18
        //     background: Rectangle{
        //         color: btnColor
        //             }
        //     onClicked: {
        //         //miniKeyBoard.lineValue = "";
        //         //miniKeyBoard.textInput=oldPwd;
        //     }
        // }

        Rectangle {
            id: recCurrentValue
            width:180
            height:30
            // x: 218
            // y: 7
            color: "transparent"
            border.width: 0
            border.color: "white"
            Layout.row:0
            Layout.column:2
            Layout.columnSpan: 2
            Layout.preferredWidth: 180
            Layout.preferredHeight: 30
            // 绘制一条边框
//            Shape {
//                anchors.fill: parent
//                ShapePath {
//                    strokeWidth: 3
//                    strokeColor: "white"
//                    strokeStyle: ShapePath.SolidLine
//                    startX: 0
//                    startY: recCurrentValue.height
//                    PathLine {
//                        x: recCurrentValue.width
//                        y: recCurrentValue.height
//                    }
//                }
//            }
            TextInput{
                id:currentValue
                anchors.fill: parent
                //text:qsTr("电压:")+mymodbus.current0.toString()+"mV"
                //echoMode: TextInput.Password
                color:"white"
                font.pixelSize: 20
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignBottom
                readOnly: true
                MouseArea {
                       anchors.fill: parent
                       onClicked: {
                           // miniKeyBoard.lineValue = "";
                           // miniKeyBoard.textInput=oldPwd;
                       }
                   }

            }
        }
        Rectangle {
                id:stableLight
                Layout.row:0
                Layout.column:4
                Layout.preferredWidth: 30
                Layout.preferredHeight: 30
                z:100
                //anchors.horizontalCenter: parent.horizontalCenter
                //y: 125;                  // 位于父窗体的y位置，以左上角为起点，缺省为0
                //width: 30;            // 宽度，此时Rectangle控件在Window控件内，属于Window的孩子，可以使用parent继承
                //height: 30;           // 高度，此时Rectangle控件在Window控件内，属于Window的孩子，可以使用parent
                color: "transparent";         // 颜色，缺省为白色
                opacity: 1;          // 透明度，缺省为1
                radius: 15;           // 圆角，通过圆角来画出一个圆形窗体出来
                //clip: true;            // 截断，在Rectangle控件内的子控件，超出他本身大小后会被截断
            }

    }
    }
    Text {
        id:gainCalTip
        x:0
        y:150
        text:qsTr("请先放置砝码")+"\n"+qsTr("砝码重量大于量程1/3")+"\n"+qsTr("输入砝码重量值")+"\n"+qsTr("待指示灯变绿")+"\n"+qsTr("点击“确认”")+"\n"+qsTr("完成增益标定")
        //wrapMode: Text.Wrap
        color:"white"
        font.pixelSize: 20
        horizontalAlignment: Text.AlignLeft
        verticalAlignment: Text.AlignBottom
    }
    MiniKeyBoard
    {
        id: miniKeyBoard
        width:800
        //x: 34
        y: 126
        isNumber: true
        textInput: curValue
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: gainCalLoader
        visible:false
    }
    Dialog {
        anchors.centerIn: parent
        id: gainCalOKDialog
        modal:true
        closePolicy: "CloseOnEscape"
        //title: "提示"
        standardButtons: Dialog.Ok
        title:qsTr("校准成功！")
        onAccepted: {
            gainCalOKDialog.close()
//            if(gainCalOK){
//            }
        }
        z:100
        //onRejected: console.log("Cancel clicked")
    }
    Button
    {
        id:btnGainCalOK
        x: 572
        y: 281
        //x: 675
        //y: 20
        width: 100
        height: 100
        background: Rectangle{
            border.width: 0
            color: "#C070B0"
                }

        text: qsTr("确定")
        font.pixelSize: 25
        palette.buttonText: "white"
        Connections {
            target: btnGainCalOK
            function onClicked() {
                gainCalOK=false
                if(curValue.text.length == 0)
                {
                    gainCalOKDialog.title=qsTr("请输入当前值！")
                    gainCalOKDialog.open()
                    return;
                }
                var currentWeight =Number(curValue.text)
                if(currentWeight<0)
                {
                    gainCalOKDialog.title=qsTr("输入值不能小于0！")
                    gainCalOKDialog.open()
                    return;
                }
//                if(currentWeight > mymodbus.maxRange)
//                {
//                    gainCalOKDialog.title=qsTr("输入值不能大于量程！")
//                    gainCalOKDialog.open()
//                    return;
//                }
                gainCalLoader.visible=true
                if(mymodbus.gainCalibration(cmbGainIndex.currentIndex,currentWeight)){
                    gainCalOK=true
                    gainCalOKDialog.title=qsTr("增益校准成功！")
                }
                else{
                     gainCalOKDialog.title=qsTr("增益校准失败！")
                }
                gainCalLoader.visible=false
                gainCalOKDialog.open()
                //miniKeyBoard.buttonClean()
//                rectangle.selectedView = 2
//                bottomBar.selected=2
            }
        }
    }

    Button
    {
        id:btnReturn
        x: 688
        y: 281
        //x: 675
        //y: 20
        width: 100
        height: 100
        background: Rectangle{
            border.width: 0
            color: "#B0B0D0"
                }

        text: qsTr("返回")
        font.pixelSize: 25
        palette.buttonText: "white"
        Connections {
            target: btnReturn
            function onClicked() {
                bottomBar.selected=2
//                myLoader.visible=false;
//                myLoader.visible=true
                myLoader.source="CalPanel.qml"
            }
        }
    }
    Timer{
        id:getVoltageCurrentTimer
        running: false
        repeat: true
        //interval: 500
        onTriggered: {
            //mymodbus.getVoltageById
            currentValue.text= qsTr("电压:")+mymodbus.voltage+"uV"
            stableLight.color = mymodbus.stable===1? "green":"red";
           //currentValue.text= qsTr("电压:")+mymodbus.voltage(deviceIndex.currentIndex).toString()+"uV"
        }
    }
}

